<?php

namespace app\admin\model\wanlshop;

use app\api\model\wanlshop\OrderGift;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'wanlshop_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'createtime_text',
        'paymenttime_text',
        'delivertime_text',
        'dealtime_text',
        'status_text',
    ];

    /**
     * 获取订单类型
     * @param $id
     * @return string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getOrderType($id)
    {
        $data = self::findOrFail($id);
        $type = '';

        //换购订单
        if ($data['score'] > 0) {
            return '积分置换';
        }
        if ($data['coupon_id'] > 0) {
            return '优惠券置换';
        }
        //use_invoice字段暂时显示的是线下实体店置换
        if ($data['invoice_id'] > 0) {
            return '发票抵扣置换';
        }
        if ($data['offline_order'] == 1) {
            return '线下实体店置换';
        }
        if ($data['order_type'] == 1) {
            return '平台旧件置换';
        }
        if ($data['order_type'] == 0) {
            return '普通订单';
        } elseif ($data['order_type'] == 1) {
        } elseif ($data['order_type'] == 2) {
            return '非平台旧件置换';
        }
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3'), '4' => __('State 4'), '5' => __('State 5'), '6' => __('State 6'), '7' => __('State 7'), '8' => __('State 8'), '9' => __('State 9'), '10' => __('State 10')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getOrderTypeList()
    {
        return ['0' => __('普通订单'), '1' => __('换购订单'), '2' => __('图片换购')];
    }

    public function getStateTextAttr($value, $data)
    {
        $value = $value ?: ($data['state'] ?? '');
        $list = $this->getStateList();
        return $list[$value] ?? '';
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['createtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getPaymenttimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['paymenttime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getDelivertimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['delivertime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getDealtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['dealtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    protected function setPaymenttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDelivertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDealtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo('app\admin\model\wanlshop\Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function ordergoods()
    {
        return $this->hasMany('app\admin\model\wanlshop\OrderGoods', 'order_id', 'id', [], 'LEFT');
    }

    public function coupon()
    {
        return $this->belongsTo('app\admin\model\wanlshop\CouponReceive', 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function pay()
    {
        return $this->hasOne(Pay::class, 'order_id', 'id');
    }

    public function gifts()
    {
        return $this->hasMany(OrderGift::class, 'order_id', 'id');
    }
}
